//
//  ViewController.m
//  QZTabBarContentView
//
//  Created by admin on 2017/8/10.
//  Copyright © 2017年 QZ. All rights reserved.
//


#import "HomeViewController.h"
#import <Masonry/Masonry.h>
#import "NewsTableViewController.h"
#import "HomeViewController+LoadData.h"

@interface HomeViewController () <QZTabBarContentViewDataSource,QZTabBarContentViewDelegate>

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleImageView];
    self.tabBarContentView.dataSource = self;
    self.tabBarContentView.delegate = self;
    [self setupTabBar];//解决自动的向下偏移64像素
    [self loadChannelData];

}
///创建Title图片
- (void)setupTitleImageView {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 69, 21)];
    imageView.image = [UIImage imageNamed:@"title"];
    self.navigationItem.titleView = imageView;
    
    UIButton * buttonRightItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,70,NAVIGATION_BAR_HEIGHT)];
    [buttonRightItem setImage:[UIImage imageNamed:@"24消失热文"] forState:UIControlStateNormal];
    [buttonRightItem addTarget:self action:@selector(clickReWen) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:buttonRightItem];
    [self.navigationItem setLeftBarButtonItem:rightBarButton];
}
- (void)clickReWen {
    Class clz = NSClassFromString(@"LSZHotVC");//将字符串转化成class
    UIViewController *viewController = [[clz alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}
///解决自动的向下偏移64像素
- (void)setupTabBar {
    // iOS7.0的特性 : 当滚动视图及其子类(UIScrollView/UIWebView/UITablewView/UICollectionView/UITexeView...)在自动布局时,遇到导航,里面的内容会自动的向下偏移64像素.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.translucent = NO;//设置为不半透明也可以解决灰色的问题
    self.tabBarController.tabBar.translucent = NO;//设置为不半透明也可以解决灰色的问题
}

#pragma mark - QZTabBarContentViewDelegate -

-(UIColor *)highlightColorForTabBarItemInTabBarContentView:(QZTabBarContentView *)tabBarContentView{
    //0 0.709804 0 1
    return [UIColor colorWithRed:0 green:0.709804  blue:0 alpha:1.0];
}
-(CGFloat)heightForTabBarInTabBarContentView:(QZTabBarContentView *)tabBarContentView{
    return 44;
}
-(UIColor *)colorForTabBarItemTextInTabBarContentView:(QZTabBarContentView *)tabBarContentView{
    return [UIColor blackColor];
}
#pragma mark - QZTabBarContentViewDataSource -
/**
 放回当前控件需要多少个选项
 @param tabBarContentView 当前控件
 @return 返回选项个数
 */
- (NSInteger)numberOfItemsInTabBarContentView:(QZTabBarContentView *)tabBarContentView {
    return _array.count;
}
/**
 提供选项的标题
 
 @param tabBarContentView 当前控件
 @param index 标题索引
 @return 返回标题内容
 */
- (NSString *)tabBarContentView:(QZTabBarContentView *)tabBarContentView titleForItemAtIndex:(NSInteger)index {
    return [NSString stringWithFormat:@"%@",_array[index].cate];
//    return [NSString stringWithFormat:@"item %zd",index];
}

/**
 提供内容视图
 @param tabBarContentView 当前控件
 @param index 标题索引
 @return 内容视图
 */
- (UIView *)tabBarContentView:(QZTabBarContentView *)tabBarContentView contentViewAtIndex:(NSInteger)index {
    
    NSString *cid = _array[index].id;

    NSLog(@"cid = %@", cid);
    _newsVC = [[NewsTableViewController alloc] init];
    _newsVC.cid = cid;
    if (index == 0) {
        _newsVC.url = cid;
    }
    [self addChildViewController:_newsVC];
    return _newsVC.view;
}

-(void)goToLogin{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"AccLogin" bundle:nil];
    UIViewController *controller = [sb instantiateInitialViewController];
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark - QZTabBarContentViewDelegate -

/**
 返回一个当前选项选中的视图
 
 @param tabBarContentView 当前控件
 @return 选中的视图
 */
- (UIView *)highlightViewForTabBarItemInTabBarContentView:(QZTabBarContentView *)tabBarContentView {
    UIView *view = [UIView new];
    
    UIView *line = [UIView new];
    line.backgroundColor = [UIColor colorWithRed:0 green:0.709804  blue:0 alpha:1.0];
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.bottom.equalTo(view);
        //        make.centerX.equalTo(view);
        //        make.width.mas_equalTo(44);
        make.left.bottom.right.equalTo(view);
        make.height.mas_equalTo(2);
    }];
    
    return view;
}

/**
 当选中状态发生改变时回调
 
 @param tabBarContentView 当前控件
 @param index 选项下标
 */
- (void)tabBarContentView:(QZTabBarContentView *)tabBarContentView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"index:%zd",index);
    NSString *cid = _array[index].id;
    NSLog(@"cid = %@", cid);
    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"didSelectChannelAtCid" object:nil userInfo:@{@"cid":cid}]];
//    [[NSUserDefaults standardUserDefaults] setObject:cid forKey:@"didSelectChannelAtCid"];
}

@end
